<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Utils;

/**
 * Description of Utils
 *
 * @author bondcs
 */
class Utils {
    
    protected $container;
    
    public function __construct($container) {
        $this->container = $container;
    }

    public function strUp($valor) {

        $valor = mb_strtoupper($valor, 'UTF-8');
        return $valor;
    }
    
    public static function conv_data_to_us($date){
	$dia = substr($date,0,2);
	$mes = substr($date,3,2);
	$ano = substr($date,6,4);
	return "{$ano}-{$mes}-{$dia}";
    }
    
    public function strForDate($str){
        
        switch ($str){
            case "h":
                return array("dtInicio" => new \Datetime("-1 days"),
                             "dtTermino" => new \Datetime());
                break;
            
            case "mp":
                return array("dtInicio" => new \DateTime('first day of last month today'),
                             "dtTermino" => new \DateTime('last day of last month midnight'));
                break;
            
            case "7":
                return array("dtInicio" => new \Datetime("-7 days"),
                             "dtTermino" => new \Datetime());
                break;
            case "14";
                return array("dtInicio" => new \Datetime("-14 days"),
                             "dtTermino" => new \Datetime());
                break;
            case "30";
                return array("dtInicio" => new \Datetime("-30 days"),
                             "dtTermino" => new \Datetime());
                break;       
        }
        
    }
    
    function CalculaMOD10($Valor)
    {
        $Soma = 0;
        $Len = strlen($Valor);
        for ($i = 0; $i < $Len; $i++) {
            $N = $Valor[$i];
            if ((($i+1) % 2) == 1) {
                $Soma = $Soma + ($N*1);
            } else {
                $tmp = (string)($N*2);
                for ($j = 0; $j < strlen($tmp); $j++) {
                    $Soma = $Soma + $tmp[$j];
                }
            }
        }

        $Result = 10 - ($Soma % 10);
        if ($Result >= 10) {
            $Result = 0;
        }
        return $Result;
    }
    
    function replace_accents($string)
    {
        return str_replace( array('à','á','â','ã','ä', 'ç', 'è','é','ê','ë', 'ì','í','î','ï', 'ñ', 'ò','ó','ô','õ','ö', 'ù','ú','û','ü', 'ý','ÿ', 'À','Á','Â','Ã','Ä', 'Ç', 'È','É','Ê','Ë', 'Ì','Í','Î','Ï', 'Ñ', 'Ò','Ó','Ô','Õ','Ö', 'Ù','Ú','Û','Ü', 'Ý', '*'), array('a','a','a','a','a', 'c', 'e','e','e','e', 'i','i','i','i', 'n', 'o','o','o','o','o', 'u','u','u','u', 'y','y', 'A','A','A','A','A', 'C', 'E','E','E','E', 'I','I','I','I', 'N', 'O','O','O','O','O', 'U','U','U','U', 'Y'), $string);
    } 
    
    public function formataLiberacao($liberacao, $versao)
    {
        if ($versao[0] == 4){
            $string1 = substr_replace($liberacao," - ",7,0);
            $string2 = substr_replace($string1," - ",17,0);
            $string3 = substr_replace($string2," - ",27,0);
            return mb_strtoupper($string3, 'UTF-8');
        }elseif ($versao[0] >= 5){
            $string1 = substr_replace($liberacao," - ",5,0);
            $string2 = substr_replace($string1," - ",13,0);
            $string3 = substr_replace($string2," - ",21,0);
            return mb_strtoupper($string3, 'UTF-8');
        }else{
            return mb_strtoupper($liberacao,'UTF-8');
        }
    }
    
    public function criptografa($codigo, $size, $mudarPos) {
        
        $pos = array(0,11,10,6,5,1,9,4,3,2,8,7);
        $crip[] = array(9,1,3,5,7,6,8,0,2,4);
        $crip[] = array(7,9,1,3,5,2,4,6,8,0);
        $crip[] = array(5,7,9,1,3,8,0,2,4,6);
        $crip[] = array(3,5,7,9,1,4,6,8,0,2);
        $crip[] = array(1,3,5,7,9,0,2,4,6,8);
        $crip[] = array(6,8,0,2,4,9,1,3,5,7);
        $crip[] = array(2,4,6,8,0,7,9,1,3,5);
        $crip[] = array(8,0,2,4,6,5,7,9,1,3);
        $crip[] = array(4,6,8,0,2,3,5,7,9,1);
        $crip[] = array(0,2,4,6,8,1,3,5,7,9);
        
	$tmpCodigo = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	
        //var_dump($codigo);
	for ($i = 0; $i < $size; $i++){
		if ($mudarPos)
			$tmpCodigo[$i] = (int)$codigo[$pos[$i]];
		else
			$tmpCodigo[$i] = (int)$codigo[$i];
        }

	for ($i = 1; $i < $size; $i++)
		$tmpCodigo[$i] = $crip[$tmpCodigo[$i-1]][$tmpCodigo[$i]];
        
        
	for ($i = 0; $i < $size; $i++){
            $outString[$i] = $tmpCodigo[$i];
        }
        
        return implode("", $outString);
     }

     public function descriptografa($codigo, $size, $mudarPos) {
         
        $pos = array(0,11,10,6,5,1,9,4,3,2,8,7);
        $crip[] = array(9,1,3,5,7,6,8,0,2,4);
        $crip[] = array(7,9,1,3,5,2,4,6,8,0);
        $crip[] = array(5,7,9,1,3,8,0,2,4,6);
        $crip[] = array(3,5,7,9,1,4,6,8,0,2);
        $crip[] = array(1,3,5,7,9,0,2,4,6,8);
        $crip[] = array(6,8,0,2,4,9,1,3,5,7);
        $crip[] = array(2,4,6,8,0,7,9,1,3,5);
        $crip[] = array(8,0,2,4,6,5,7,9,1,3);
        $crip[] = array(4,6,8,0,2,3,5,7,9,1);
        $crip[] = array(0,2,4,6,8,1,3,5,7,9);
         
         
        $tmpCodigo = array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

        for ($i = 0; $i < $size; $i++){
            $tmpCodigo[$i] = (int)$codigo[$i];
        }
        
        //var_dump($tmpCodigo);
        
        for ($i = $size - 1; $i >= 1; $i--) {
            for ($j = 0; $j < 10; $j++) {
                if ($crip[$tmpCodigo[$i-1]][$j] == $tmpCodigo[$i]) {
                    $tmpCodigo[$i] = $j;
                    break;
                }
            }
        }
        
        //var_dump($tmpCodigo);
        $out = "";
        
        for ($i = 0; $i < $size; $i++)
        if ($mudarPos)
            $out[$pos[$i]] = $tmpCodigo[$i];
        else
            $out[$i] = $tmpCodigo[$i];
        
        ksort($out);
        
        return implode("", $out);
     }
     
     public function floordec($zahl,$decimals=2){   
        return floor($zahl*pow(10,$decimals))/pow(10,$decimals);
     }
 
}

?>
